<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>
        </title>
         <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="format-detection" content="telephone=no">
        <link rel="stylesheet" type="text/css" href="layui/css/layui.css" />
		<script src="layui/layui.js" type="text/javascript" charset="UTF-8"></script>
        <link rel="stylesheet" href="./css/x-admin.css" media="all">
    </head>
    <body>
         <!-- 顶部 -->
	<!--
        	作者：offline
        	时间：2019-12-31
        	描述：添加div
        -->
        <!-- 角色管理 -->
      	<!-- 穿梭框 -->
	<div id="roles_div" style="display: none;text-align: center;margin-top: 20px;">
	<div id="role_transfer"></div>
	</div>
	<div id="addDiv" style="display: none; margin-left: 10px;">
		<form id="addForm" class="layui-form" action="" method="post"
			onsubmit="return false;">
			<div class="layui-form-item">
				<label class="layui-form-mid">用户姓名：</label>
				<div class="layui-input-inline" style="width: 350px;">
					<input class="layui-input" name="uloginname"
						lay-verify="required|ulginname" />
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-mid">密保手机：</label>
				<div class="layui-input-inline" style="width: 350px;">
					<input class="layui-input" name="uphone"
						lay-verify="required|uphone" />
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-mid">用户邮箱：</label>
				<div class="layui-input-inline" style="width: 350px;">
					<input class="layui-input" name="uemail"
						lay-verify="required|uemail" />
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-mid">是否锁定：</label>
				<div class="layui-input-block">
					<input type="radio" name="uislockout" value="是" title="是"> <input
						type="radio" name="uislockout" value="否" title="否">
				</div>
			</div>
			<div class="layui-inline">
				<label class="layui-form-label">日期</label>
				<div class="layui-input-inline">
					<input type="text" id="test1" name="ucreatime" class="layui-input">
				</div>
			</div>

			<div class="layui-form-item" style="text-align: center;">
				<button id="addBtn" class="layui-btn">添加</button>
			</div>
		</form>
	</div>

	<!--
        	作者：offline
        	时间：2019-12-31
        	描述：修改div
        -->
	<div id="updateDiv" style="display: none; margin-left: 10px;">
		<form id="updateForm" lay-filter="updateForm" class="layui-form"
			action="" method="post" onsubmit="return false;">
			<div class="layui-form-item">
				<label class="layui-form-mid">用户名稱：</label>
				<div class="layui-input-inline" style="width: 350px;">
					<input class="layui-input" type="hidden" name="uid" /> <input
						class="layui-input" name="uloginname"
						lay-verify="required|ulginname" />
				</div>
			</div>
			

			<div class="layui-form-item" style="text-align: center;">
				<button id="updateBtn" class="layui-btn">修改</button>
			</div>
		</form>
	</div>


<!-- sousuo -->
	<div class="layui-form-item">
		<label class="layui-form-label">用户名称</label>
		<div class="layui-input-inline">
			<input type="text" id="student_name" class="layui-input">
		</div>
		<label class="layui-form-label">日期</label>
				<div class="layui-input-inline">
			<input type="text" id="ucreatime" class="layui-input">
				</div>
		<button class="layui-btn" id="search">搜索</button>

	</div>


	<!--
        	作者：offline
        	时间：2019-12-31
        	描述：头部按钮
        -->
	<script type="text/html" id="top">
			<button class="layui-btn" lay-event="add">添加</button> 
		</script>
	<!--
        	作者：offline
        	时间：2019-12-31
        	描述：右侧按钮
        -->
	<script type="text/html" id="rightBar">
			<button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="role">角色</button>
			<button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="resetpwd">密码重置</button>
			<button class="layui-btn layui-btn-radius layui-btn-normal" lay-event="lock">是否锁定</button> 
			<button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="update">修改</button> 
			<button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="del">删除</button>
		</script>


	<table id="demo" class="layui-hide" lay-filter="tfil"></table>


	<script type="text/javascript">
		layui.use([ 'table', 'layer', 'form', 'laydate','transfer'], function() {
			var table = layui.table;
			var layer = layui.layer;
			var form = layui.form;
			var $ = layui.$;
			var laydate = layui.laydate;
			var transfer = layui.transfer;// 穿梭框
			laydate.render({
				elem : "#test1"
			})
			laydate.render({
				elem : "#ucreatime"
			})
			table.render({
				elem : "#demo",
				url : "getAll",
				page : true,
				toolbar : '#top',
				cols : [ [  {
					field : "ucreatime",
					title : "创建时间"
				}
				, {
					field : "uemail",
					title : "用户邮箱"
				}
				, {
				field : "uislockout",
				templet:function(d) {
	                    if (d.uislockout=="是"){
	                        return "<button  class='layui-btn layui-btn-danger layui-btn-mini'>已锁定</button>";
	                    } else if (d.uislockout =="否") {
	                        return "<button  class='layui-btn layui-btn-normal layui-btn-mini'>未锁定</button>";
	                    }
	           },title:"是否锁定"}
				, {
					field : "uloginname",
					title : "登录名称"
				}
				, {
					field : "uphone",
					title : "密保手机"
				}
				,  {
					title : "操作",
					toolbar : "#rightBar"
				} ] ],
				id : "tab"
			})
			//添加监听
			table.on("toolbar(tfil)", function(obj) {
				if (obj.event == "add") {
					layer.open({
						type : 1,
						title : '添加',
						content : $("#addDiv"),
						area : [ '550px', '600px' ]
					})
				}
			});
			//模糊查询
			active = {
					reload : function() {
						var uloginname = $("#student_name").val();
						var ucreatime = $("#ucreatime").val();
						table.reload("tab",{
							where : {
								"uloginname" : uloginname,
								"ucreatime":ucreatime
							}
						})
					}
				}
				$("#search").click(function() {
					active.reload();
				})
			$("#addBtn").click(function() {
				$.post("addUser", $("#addForm").serialize(), function(res) {
					if (res="1") {
						layer.msg("sucess");
						layer.closeAll();
						active.reload();
					}
				}, "text")
			})

			//修改监听
			table.on("tool(tfil)", function(obj) {
				if (obj.event == "update") {
					form.val("updateForm", obj.data);
					layer.open({
						type : 1,
						title : '修改',
						content : $("#updateDiv"),
						area : [ '550px', '600px' ]
					})
				}
				
				else if(obj.event=="resetpwd"){//重置密码
					layer.confirm("是否重置"+obj.data.uloginname+"的密码", {
							btn: ['重置', '取消']
						}, function(index, layero) {
							$.ajax({
								url:"resetPwd",
								data: {id : obj.data.uid},
								success : function(result) {
									layer.alert(result);
									active.reload();
								}
							});
						});
				}//是否锁定
				else if(obj.event=="lock") {
					layer.confirm('是否修改锁定状态', {
							btn: ['修改', '取消']
						}, function(index, layero) {
							$.ajax({
								url:"updateLock",
								data: {id : obj.data.uid,lock:obj.data.uislockout},
								success : function(result) {
									layer.alert(result);
									active.reload();
								}
							});
						});
				}
				else if (obj.event == "del") {//删除
					$.post("delUser", {
						uid : obj.data.uid
					}, function(res) {
						if (res = "1") {
							layer.msg("sucess");
							layer.closeAll();
							active.reload();
						}
					}, "text");
				}
				else if(obj.event=="role"){
					var data=obj.data;
					layer.open({
						type: 1,
						title: '您正在设置用户[' + obj.data.uloginname + ']的角色',
						area: ['600px', '600px'],
						content: $("#roles_div")
					});
					var rdata = new Array();
					$.get('getall1',function(rs){
						$.get('getuseridfindRoles', {
							'uid': obj.data.uid,
						},function(res){
							if(res.length>0){
								for (var i = 0; i < res.length; i++) {
										rdata.push(res[i].rid);
								}
							}
							console.log(res)
							transfer.render({
								elem: '#roles_div',
								title: ['系统所有角色', '当前用户角色'],
								data: rs,
								value:rdata,
								parseData:function(r){
									return {
										"value":r.rid,
										"title":r.rname
									}
								}
							,
								onchange:function(obj,index) {
								 if (index=='0'){
									for(var i=0;i<obj.length;i++){
													$.post('AddUserToRole',{
														"uid":data.uid,
														"rid":obj[i].value
													},function(res){
														
													},"json")
												} 
									}
									else{
										for(var i=0;i<obj.length;i++){
															$.post('delUserToRole',{
																'uid':data.uid,
																'rid':obj[i].value,
															},function(res){
										
															},"json")
														} 
									}
								}
							})
						}, 'json');
					}, "json")
				}
			});
			$("#updateBtn").click(
					function() {
						$.post("addUser", $("#updateForm").serialize(),
								function(res) {
									if (res = "1") {
										layer.msg("sucess");
										layer.closeAll();
										active.reload();
									}
								}, "text")
					})

		})
	</script>
    </body>
</html>